package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.example.annotation.PassToken;
import com.example.entity.AreaTime.BaseRetBean;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ChenXiaoBin
 * on 2019-07-16
 */

@RestController
@RequestMapping("/V1.0/date")
public class DateController {

    @PutMapping("")
    @ApiOperation(value = "测试时间工具类", notes = "测试时间工具类")
    @PassToken
    public void dateTest() {
        //指定时间延后
        Date das = new Date();
        System.out.println(das);
        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(das);
//        calendar.add(Calendar.DATE,10);
//        Date date = calendar.getTime();
        //获取本月的第一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstday = format.format(calendar.getTime());
        Date beginDay = null;
        try {
            beginDay = format.parse(firstday);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //当前月最后一天
        calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        String lastday = format.format(calendar.getTime());
        try {
            Date endDay = format.parse(lastday);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //用于格式化时间
//        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
//        Date eitTime = null;
//        String df = null;
//        try {
//            eitTime = sdf.parse("08:00");
//            df = sdf.format(new Date());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(eitTime);
//
//        System.out.println("string" + df);
    }

    @PutMapping("/hutool")
    @PassToken
    @ApiOperation(value = "hutool时间工具类", notes = "hutool时间工具类")
    public void hutoolUtil() {
        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        String now = DateUtil.now();
    }


    /**
     * 获取指定月份的第一天与最后一天
     * @param
     * @param
     * @return
     */
    @GetMapping("/getLocalMonthWeekList")
    @PassToken
    public void getLocalMonthWeekList(){
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        int startyear = 2019;
        int lastmonth = 11;
        Calendar calstar = Calendar.getInstance();
        calstar.set(Calendar.YEAR, startyear);
        calstar.set(Calendar.MONTH, lastmonth);
//        calstar.set(Calendar.DAY_OF_MONTH, 1);
        calstar.set(Calendar.DAY_OF_MONTH, 0);//最后一天
        String star = sm.format(calstar.getTime());
    }


    private List<String[]> converWeekList(LocalDate startDate,LocalDate endDate){

        List<String[]> weekList = new ArrayList<>();
        //转换成joda-time的对象
        LocalDate firstDay = startDate;
        LocalDate lastDay = endDate;
        //计算两日期间的区间天数
        Period p = new Period(firstDay, lastDay, PeriodType.days());
        int days = p.getDays();
        if (days > 0){
            int weekLength = 7;
            for(int i=0;i<days;i=i+weekLength){
                String monDay = firstDay.plusDays(i).toString("yyyy-MM-dd");
                String sunDay = firstDay.plusDays(i+6).toString("yyyy-MM-dd");
                String [] week = {monDay,sunDay};
                weekList.add(week);
            }
        }
        return weekList;
    }







    public static void main(String[] arg) {
        GregorianCalendar d = new GregorianCalendar();
        int today = d.get(Calendar.DAY_OF_MONTH);
        int month = d.get(Calendar.MONTH);

        d.set(Calendar.DAY_OF_MONTH, 1);
        int weekday = d.get(Calendar.DAY_OF_WEEK);

        int firstDayOfWeek = d.getFirstDayOfWeek();

        int indent = 0;
        while (weekday != firstDayOfWeek) {
            indent++;
            d.add(Calendar.DAY_OF_MONTH, -1);
            weekday = d.get(Calendar.DAY_OF_WEEK);
        }

        String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
        do {
            System.out.printf("%4s", weekdayNames[weekday]);
            d.add(Calendar.DAY_OF_MONTH, 1);
            weekday = d.get(Calendar.DAY_OF_WEEK);
        } while (weekday != firstDayOfWeek);

        System.out.println();

        for (int i = 0; i < indent; i++){
            System.out.print("    ");
        }
        d.set(Calendar.DAY_OF_MONTH, 1);

        do {
            int day = d.get(Calendar.DAY_OF_MONTH);
            System.out.printf("%3d", day);

            if (day == today) {
                System.out.printf("*");
            } else {
                System.out.printf(" ");
            }
            d.add(Calendar.DAY_OF_MONTH, 1);
            weekday = d.get(Calendar.DAY_OF_WEEK);

            if (weekday == firstDayOfWeek) {
                System.out.println();
            }
        } while (month == d.get(Calendar.MONTH));

        if (weekday != firstDayOfWeek) {
            System.out.println();
        }
    }
}
